/**
 * Project Name:mauto-web
 * File Name:AreaService.java
 * Package Name:com.inftech.mauto.sys.service
 * Date:2016年10月7日下午9:14:13
 * Copyright (c) 2016, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.core.service;

import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.inftech.mauto.common.base.BusinessException;
import com.inftech.mauto.common.constant.BusinessConstant;
import com.inftech.mauto.common.constant.HttpStatusCodeEnum;
import com.inftech.mauto.common.utils.CommonUtils;
import com.inftech.mauto.core.dao.IEnquiryDao;
import com.inftech.mauto.core.entity.Enquiry;
import com.mingsoft.base.biz.impl.BaseBizImpl;
import com.mingsoft.base.dao.IBaseDao;
import com.mingsoft.weixin.dao.IWeixinDao;
import com.mingsoft.weixin.entity.WeixinEntity;
import com.mingsoft.weixin.util.KfSessionUtils;
import com.mingsoft.weixin.util.MessageUtils;

/**
 * 
 * ClassName: EnquiryService <br/>
 * Function: 车大夫咨询service. <br/>
 * date: 2016年11月6日 下午10:24:00 <br/>
 *
 * @author puyf
 * @version
 * @since JDK 1.7
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Service("enquiryService")
public class EnquiryService extends BaseBizImpl {

	/**
	 * 咨询持久化层注入
	 */
	@Autowired
	private IEnquiryDao enquiryDao;

	/**
	 * 注入微信持久化层
	 */
	@Autowired
	private IWeixinDao weixinDao;

	/**
	 * 前台客服评价链接地址
	 */
	@Value("${commentUrlC}")
	private String commentUrlC;

	@Override
	protected IBaseDao getDao() {
		return enquiryDao;
	}

	/**
	 * 
	 * saveEnquiry:客服咨询 . <br/>
	 *
	 * @author puyf
	 * @param peopleId
	 * @param openId
	 * @param csAccount
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public void saveEnquiry(int peopleId, String openId, String csAccount) throws Exception {
		// 获取微信信息
		WeixinEntity weixin = weixinDao.getEntity(null, null, null);
		KfSessionUtils kfSessionUtils = new KfSessionUtils(weixin.getWeixinAppID(), weixin.getWeixinAppSecret());
		// 判断是否存在会话
		String kfAccount = kfSessionUtils.getKfSession(openId);
		if (!kfAccount.equals("")) {
			// 已存在会话
			LOG.warn("当前存在未关闭的会话");
			throw new BusinessException(HttpStatusCodeEnum.STATUS_CODE_911);
		} else {
			// 不存在会话，创建会话
			Boolean createSuccess = kfSessionUtils.createKfSession(csAccount, openId);
			MessageUtils messageUtils = new MessageUtils(weixin.getWeixinAppID(), weixin.getWeixinAppSecret());
			if (createSuccess) {
				// 创建成功
				messageUtils.sendTextToUser(openId, "欢迎使用在线咨询，请问您的爱车出现了什么问题？");
				// 插入数据库
				Enquiry enquiry = new Enquiry();
				Date now = CommonUtils.getNowD();
				enquiry.setEnquiryOpenId(openId);
				enquiry.setEnquiryKfAccount(csAccount);
				enquiry.setEnquiryCreateTime(now);
				enquiry.setEnquiryOverTime(null);
				enquiry.setEnquiryCommentStatus(BusinessConstant.COMMENT_STATUS_OFF);
				enquiry.setEnquiryCommentTime(null);
				enquiry.setCreateBy(peopleId);
				enquiry.setCreateDate(now);
				enquiry.setUpdateDate(now);
				enquiry.setDelFlag(0);
				enquiryDao.saveEntity(enquiry);
			} else {
				// 创建失败
				// messageUtils.sendTextToUser(openId, "当前车大夫不在线，请另外选择车大夫进行咨询");
				LOG.error("车主与客服会话创建失败");
				throw new BusinessException(HttpStatusCodeEnum.STATUS_CODE_912);
			}
		}
	}

	/**
	 * 
	 * sendCommentUrl:发送评价链接. <br/>
	 *
	 * @author puyf
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public void sendCommentUrl() throws Exception {
		// 查询昨天的未评价咨询列表
		Map<String, Object> whereMap = new HashMap<String, Object>();
		whereMap.put("del_flag", 0);
		whereMap.put("enquiry_comment_status", BusinessConstant.COMMENT_STATUS_OFF);
		whereMap.put("enquiry_create_time", CommonUtils.getDate(-1));
		List<Enquiry> enquiryList = enquiryDao.queryEnquiryList(whereMap);
		if (enquiryList != null && enquiryList.size() != 0) {
			// 获取微信信息
			WeixinEntity weixin = weixinDao.getEntity(null, null, null);
			MessageUtils messageUtils = new MessageUtils(weixin.getWeixinAppID(), weixin.getWeixinAppSecret());
			for (Enquiry enquiry : enquiryList) {
				// 咨询者openId
				String openId = enquiry.getEnquiryOpenId();
				// 评价对象id
				String objectId = enquiry.getId();
				// 评价内容
				String commentObject = enquiry.getCsName();
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("objectId", objectId);
				paramMap.put("commentObject", commentObject);
				paramMap.put("commentType", BusinessConstant.COMMENT_TYPE_ZX);
				String params = URLEncoder.encode(JSONObject.toJSONString(paramMap), "utf-8");
				// 评价链接
				String url = commentUrlC + "param=" + params;
				// 发送内容
				String content = "亲，昨天的咨询还满意吧？快来给我们客服妹子点个赞吧！<a href='" + url + "'>点击这里</a>";
				messageUtils.sendTextToUser(openId, content);
				LOG.info("向openId" + openId + "推送评价链接成功");
			}
		}

	}

}
